import sys
import logging
import argparse
import os
from multiprocessing import Pool

log = logging.getLogger(__name__)
basedir = os.path.dirname(os.path.abspath(__file__))

formatter = logging.basicConfig(
    filename='{basedir}/mongodb_backup.log'.format(basedir=basedir),
    level=logging.DEBUG,
    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
    datefmt='%a, %d %b %Y %H:%M:%S')

from mongodb_backup import database_backup_main
from file_transfer import file_file_transfer
from backup_utils import get_mongodb_backup_batch_info
from backup_utils import get_ip_address

# from info_config import remote_host
# from info_config import remote_rsync_module
# from info_config import remote_rsync_port

from info_config import mongo_backup_dump_base_path
from info_config import mongo_backup_dump_for_long_run
from info_config import backup_process_thread


def get_backup_config_info_module():
    parser = argparse.ArgumentParser(description='MongoDB backup command line .',
                                     usage=None,
                                     add_help=True)
    parser.add_argument('--batch_id',
                        '-b',
                        type=str,
                        help='MongoDB备份批次',
                        required=True)
    parser.add_argument('--transfer',
                        '-t',
                        type=str,
                        help='是否传输至远程主机',
                        required=False)
    args = parser.parse_args()
    return args


def get_backup_config_list(batch_id, backup_host):
    mongodb_backup_batch_info = get_mongodb_backup_batch_info(batch_id, backup_host)
    return mongodb_backup_batch_info


def get_is_transfer_to_remote():
    try:
        config_option = get_backup_config_info_module()
        is_transfer_to_remote = config_option.is_transfer_to_remote
    except AttributeError as e:
        log.exception(str(e))
        return 0
    return is_transfer_to_remote


def main():
    config_option = get_backup_config_info_module()
    batch_id = config_option.batch_id
    is_transfer_to_remote = config_option.transfer
    backup_host = get_ip_address()
    backup_config_list = get_backup_config_list(batch_id, backup_host)
    backup_process = Pool(backup_process_thread)
    try:
        for backup_config in backup_config_list:
            log.info(backup_config)
            if is_transfer_to_remote == 1:
                backup_base_path = mongo_backup_dump_for_long_run
            else:
                backup_base_path = mongo_backup_dump_base_path
            argument = {"backup_config": backup_config, "backup_base_path": backup_base_path,
                        "is_transfer_to_remote": is_transfer_to_remote}
            backup_process.apply_async(database_backup_main, kwds=argument)
            # if is_transfer_to_remote == 1:
            #     backup_path = database_backup_main(backup_config=backup_config,
            #                                        backup_base_path=mongo_backup_dump_for_long_run)
            # else:
            #     backup_path = database_backup_main(backup_config=backup_config,
            #                                        backup_base_path=mongo_backup_dump_base_path)
            # print(backup_path)
        backup_process.close()
        backup_process.join()
    except Exception as e:
        log.exception(str(e))


if __name__ == "__main__":
    sys.exit(main())
